<?php


namespace Modules\Production\Repository;


use Modules\Production\Models\ProductionOrderDetail;

class ProductionOrderDetailRepository
{
    protected $productionOrderDetail;

    public function __construct(ProductionOrderDetail $productionOrderDetail)
    {
        $this->productionOrderDetail = $productionOrderDetail;
    }

    static public function getDetailList($request){

        //ProductionOrderDetail::

    }

    /**
     * 获取一条生产工单明细
     * @param $productionId
     * @param $materialNo
     */
    static public function findDetailBy($productionId,$materialNo){
        return ProductionOrderDetail::where('production_order_id',$productionId)->where('fmaterialid1',$materialNo)->first();
    }

    /**
     * 根据id修改明细数据
     * @param $id
     * @param $data
     * @return mixed
     */
    static public function updateById($id,$data){
        return ProductionOrderDetail::where('id',$id)->update($data);
    }

    /**
     * 字段数量自增
     * @param $id
     * @param $field
     * @param $qty
     * @return mixed
     */
    static public function IncById($id,$field,$qty){
        return ProductionOrderDetail::where('id',$id)->increment($field,$qty);
    }

    /**
     * 根据生产工单id获取全部子项
     * @param $productionId
     * @return mixed
     */
    static public function getListByProductionId($productionId){
        return ProductionOrderDetail::where('production_order_id',$productionId)->get();
    }

    /**
     * 获取工单子项领料总数
     * @param $productionId
     * @return mixed
     */
    static public function getPickQty($productionId){
        return ProductionOrderDetail::where('production_order_id',$productionId)->sum('fpickedqty');
    }

    /**
     * 获取工单详情
     * @param $params
     */
    static public function getOrderDetail($params){

        $builder = self::getDetailWhere($params);

        $limit = isset($params['limit']) ? $params['limit'] : 100;

        return $builder->paginate($limit)->toArray();

    }

    static public function getDetailWhere($params){
        $builder = new ProductionOrderDetail();

        //物料编码
        if(isset($params['fmaterialid1']) && $params['fmaterialid1'] != ''){
            $builder = $builder->where('fmaterialid1','like',"%{$params['fmaterialid1']}%");
        }

        //物料名称
        if(isset($params['fmaterialname1']) && $params['fmaterialname1'] != ''){
            $builder = $builder->where('fmaterialname1','like',"%{$params['fmaterialname1']}%");
        }

        //子项领料状态
        if(isset($params['back_status']) && $params['back_status'] != ''){
            if($params['back_status'] == 0){
                $builder = $builder->whereIn('back_status',[0,1]);
            }elseif($params['back_status'] == 1){
                $builder = $builder->where('back_status',2);
            }
        }

        //创建日期
        if(isset($params['start_date']) && $params['start_date'] != '' && isset($params['end_date']) && $params['end_date'] != ''){
            $start = strtotime($params['start_date']);
            $end = strtotime('+ 1 day',strtotime($params['end_date']));
            $builder = $builder->whereBetween('created_at',[$start,$end]);
        }

        $builder = $builder->with(['order','stockUser'])->whereHas('order',function($query) use($params){
            //工单号
            if(isset($params['fbillno']) && $params['fbillno'] != ''){
                $query = $query->where('fbillno','like',"%{$params['fbillno']}%");
            }

            //生产订单
            if(isset($params['fmobillno']) && $params['fmobillno'] != ''){
                $query = $query->where('fmobillno','like',"%{$params['fmobillno']}%");
            }

            //产品编码
            if(isset($params['fmaterialid']) && $params['fmaterialid'] != ''){
                $query = $query->where('fmaterialid','like',"%{$params['fmaterialid']}%");
            }

            //组织代码过滤
            if(isset($params['fprdorgid_number']) && $params['fprdorgid_number'] != ''){
                $query = $query->where('fprdorgid_number',$params['fprdorgid_number']);
            }

            //车间
            if(isset($params['fworkshopid_number']) && $params['fworkshopid_number'] != ''){
                $query = $query->where('fworkshopid_number',$params['fworkshopid_number']);
            }

        });

        return $builder;
    }

}
